const TIMER_AUTOSAVE_INTERVAL = 300000; //5 mins

function Timer( todo )
{
	if( typeof todo == 'undefined' )
	{
		Log.error( 'The damn todo is missing.' );
		return;
	}
	
	//create todo timer
	var timer = create_element( 'statusbarpanel', { 
					container: true, 
					cssClass : 'tdt-timer-controls'
				},
				create_element( 'button', { 
					//id : 'timer-start-'+ this.ID,
					name : 'start',
					cssClass : 'statusbarpanel-iconic start', 
					hidden: false,
					onclick : 'this.parentNode.start( )'
				} ),
				create_element( 'button', { 
					//id : 'timer-pause-'+ this.ID,
					name : 'pause',
					cssClass : 'statusbarpanel-iconic pause', 
					hidden : true,
					onclick : 'this.parentNode.pause( )'
				} ),
				create_element( 'button', {
					//id : 'timer-display-'+ this.ID,
					name : 'display',
					cssClass : 'statusbarpanel-iconic-text display',
					onmouseover: 'this.parentNode.showTooltip( )',
					tooltip : 'tdt-todo-tooltip',
//					tooltip : 'tdt-tooltip-'+ this.ID,
 					label : Time.toHMS( todo.duration )
				} ),
				create_element( 'button', {
					//id : 'timer-stop-'+ this.ID,
					name : 'stop',
					cssClass : 'statusbarpanel-iconic stop', 
					hidden : false,
					onclick : 'this.parentNode.stop( )' 
				} )
			);

	//show the timer in the status-bar
	$('status-bar').insertBefore( timer, $('tdt-timer-icon') );
	
	timer.todo				= todo;
	timer.ID				= todo.ID;
	timer.overallDuration 	= todo.duration;
	timer.current_duration	= 0;
	timer.startTime			= 0;
	timer.timerID			= null;
	timer.autosaveID		= null;
	
	timer.getChild = function( childName )
	{
//		alert( this.childNodes.length )
		for( i in this.childNodes )
		{
			if( this.childNodes[i].getAttribute( 'name' ) == childName )
			{
				return this.childNodes[i];
			} 
		}
		return null;
	}

	timer.showTooltip = function( )
	{
		Log.debug( 'Showing tooltip '+ this.todo.title );
		$('tdt-todo-tooltip-box').value = this.todo.title;
//		var tooltipContent = create_element( 'description', {
//							value : this.todo.title
//						} );
//		$('tdt-todo-tooltip-box').appendChild( tooltipContent );
	}

	timer.start = function( startDuration )
	{
		this.startTime = Time.now( );
		
		var counting = function(elem){
			return function( ){
			  elem.count( );
			}
		 }
		 var autosaving = function(elem){
			return function( ){
			  elem.autosave( );
			}
		 }

		 this.timerID = window.setInterval( counting(this), 100 );
		 this.autosaveID = window.setInterval( autosaving(this), TIMER_AUTOSAVE_INTERVAL );

		 this.getChild( 'start' ).hidden = true;
		 this.getChild( 'pause' ).hidden = false;
		 Log.debug( 'Timer for todo Id'+ this.ID + ' started' );
	}

	timer.pause = function ( )
	{
		this.overallDuration = this.count( );
		window.clearInterval( this.timerID );
		window.clearInterval( this.autosaveID );

		this.getChild( 'start' ).hidden = false;
		this.getChild( 'pause' ).hidden = true;
		Log.debug( 'Timer for todo Id'+ this.ID + ' paused' );
	}

	timer.stop = function( )
	{
		//has this even started
		if( this.startTime > 0 )
		{
			//this.overallDuration = this.count( );
			//tdt_endTodo( this.ID );
			timer.todo.duration = timer.current_duration;
			timer.todo.endTime = Time.now( );
			timer.todo.saveToFile( );

			window.clearInterval( this.timerID );
			window.clearInterval( this.autosaveID );
			Log.debug( 'Timer for todo Id'+ this.ID + ' stopped' );
		}
		else
		{
			Log.debug( 'Timer for todo Id'+ this.ID + ' never started.' );
		}
		this.parentNode.removeChild(this);
		removeItem( runningTodos, timer.todo.ID );
		return;
	}
	
	timer.autosave = function( )
	{
			timer.todo.duration = timer.current_duration;
			timer.todo.saveToFile( );
	}
	
	timer.count = function( )
	{
//		alert( Time.now( ) - this.startTime );
		this.current_duration = ( Time.now( ) - this.startTime ) + parseInt( this.overallDuration );
		//alert( tmp_duration );
		//Log.debug( time.h +":"+ time.m +":"+ time.s );

		this.getChild( 'display' ).label = Time.toHMS( this.current_duration );
	}
	
	timer.reset= function( )
	{
		window.clearInterval( this.timerID );
	
		this.startTime		= 0;
		this.overallDuration  = 0;
		this.timerID			= null;

		this.getChild( 'display' ).label = Time.toHMS( 0 );
		
		this.getChild( 'start' ).hidden = false;
		this.getChild( 'pause' ).hidden = true;
		Log.debug( 'Timer for todo Id'+ this.ID + ' reset' );
	}
}